// vite.config.ts
import path, { resolve } from 'path'
import { defineConfig, loadEnv } from 'vite'
import uni from '@dcloudio/vite-plugin-uni'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import UnoCSS from 'unocss/vite'
import MiniProgramTailwind from '@dcasia/mini-program-tailwind-webpack-plugin/rollup'
// https://vitejs.dev/config/
// export default defineConfig({
//   plugins: [uni()],
//   resolve: {
//     alias: {
//       '@': path.resolve(__dirname, 'src'),
//       '@img': path.resolve(__dirname, 'src/static/images'),
//     },
//   },
// })

export default ({ command, mode }) => {
  const env = loadEnv(mode, process.cwd());
  return defineConfig({
    plugins: [
      uni(),
      UnoCSS(),
      AutoImport({
        imports: ['vue', 'pinia', '@vueuse/core', 'uni-app'],
        // 可以选择auto-import.d.ts生成的位置，使用ts建议设置为'src/auto-import.d.ts'
        dts: 'src/auto-import.d.ts',
        // 自动生成'eslintrc-auto-import.json'文件，在'.eslintrc.cjs'的'extends'中引入解决报错
          // eslintrc: {
          //   enabled: true,
          // },
      }),
      Components({
        dts: 'src/auto-import-components.d.ts'
      }),
      MiniProgramTailwind({
        enableRpx: false
      })
    ],
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src'),
        '@img': resolve(__dirname, 'src/static/images'),
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@import "./src/common/style/index.scss";`,
        },
      },
    },
    build: {
      sourcemap: env.VITE_BUILD_SOURCEMAP === 'true',
      minify: 'terser',
      terserOptions: {
        compress: {
          drop_console: env.VITE_BUILD_DROP_CONSOLE === 'true', // 去除 console
        },
      },
      chunkSizeWarningLimit: 1500, // chunk 大小警告的限制（以 kbs 为单位）
    },
  });
};
